package com.amgl.amupush.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amgl.amupush.R;
import com.amgl.amupush.data.News;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by amglh on 2016/8/10.
 */
public class NewsListRVAdapter extends BaseRVAdapter<News, NewsListRVAdapter.NewsViewHolder> {
    public NewsListRVAdapter(Context context) {
        super(context);
    }

    private OnNewsClickListener mOnNewsClickListener;

    public void setOnNewsClickListener(OnNewsClickListener onNewsClickListener) {
        this.mOnNewsClickListener = onNewsClickListener;
    }

    @Override
    public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.rv_item_news, parent, false);
        NewsViewHolder viewHolder = new NewsViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(NewsViewHolder holder, int position) {
        News data = getItem(position);
        holder.showData(data, position);
    }

    public interface OnNewsClickListener {
        void onNewsClick(int position);
    }

    public class NewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private int position;
        private News data;

        @BindView(R.id.text_title)
        TextView textTitle;

        public NewsViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);

            itemView.setOnClickListener(this);
        }

        public void showData(News news, int position) {
            this.data = news;
            this.position = position;

            textTitle.setText(data.title);
        }

        @Override
        public void onClick(View v) {
            if (mOnNewsClickListener != null) {
                mOnNewsClickListener.onNewsClick(position);
            }
        }
    }
}
